import Action from '../core/Action';
import { SUCCESS, RUNNING } from '../constants';
import Tick from '../core/Tick';

export default class Waiter extends Action {

  waitTime: number

  constructor(waitTime: number, name: string = `Waiter[${waitTime}]`) {
    super(name);
    this.waitTime = waitTime
  }

  onOpen(tick: Tick, ...args: any[]) {
    tick.blackboard.set('startTime', Date.now(), tick.tree.id, this.id);
  }

  onTick(tick: Tick, ...args: any[]) {
    var startTime = tick.blackboard.get('startTime', tick.tree.id, this.id);
    if (Date.now() - startTime > this.waitTime) {
      this.onConsume(tick, ...args)
      return this.defaultStatus();
    }
    return RUNNING;
  }

  onConsume(tick: Tick, ...args: any[]) {

  }


  protected defaultStatus(): number {
    return SUCCESS;
  }
};
